package com.loveprogrammer.eternity.service;

import com.loveprogrammer.eternity.action.BaseAction;
import com.loveprogrammer.eternity.enums.EffectTimeEnum;
import com.loveprogrammer.eternity.model.Character;
import com.loveprogrammer.eternity.model.attack.AttackResult;
import com.loveprogrammer.eternity.model.effect.AbstractEffect;
import com.loveprogrammer.eternity.model.effect.Effect;
import com.loveprogrammer.eternity.model.effect.SlicingEffect;
import com.loveprogrammer.eternity.model.effect.SuckBloodEffect;

import java.util.*;

/**
 * Company: ClickPaaS
 *
 * @version 1.0.0
 * @description:
 * @author: 钱旭
 * @date: 2023-03-23 17:12
 **/
public class EffectService extends BaseAction {

    public static StringBuilder notifyEffect(Character character, EffectTimeEnum effectTimeEnum,
                                             AttackResult entity, List<? extends Character> attackList,
                                             Character... defence) {
        List<AbstractEffect> effects = character.getEffects();
        StringBuilder result = new StringBuilder();
        if(effects == null){
            return result;
        }
        Map<String,AbstractEffect> effcetMap = new HashMap<>();
        // 当前只有两个
        for (AbstractEffect effect : effects) {
            if(effect instanceof SlicingEffect){
                SlicingEffect slicing = (SlicingEffect) effect;
                SlicingEffect aggregate = (SlicingEffect) effcetMap.get("SlicingEffect");
                if(aggregate == null){
                    aggregate = new SlicingEffect(UUID.randomUUID().toString(),0);
                }
                aggregate.setPercent(aggregate.getPercent() + slicing.getPercent());
                effcetMap.put("SlicingEffect",aggregate);
            }
            else if(effect instanceof SuckBloodEffect){
                SuckBloodEffect slicing = (SuckBloodEffect) effect;
                SuckBloodEffect aggregate = (SuckBloodEffect) effcetMap.get("SuckBloodEffect");
                if(aggregate == null){
                    aggregate = new SuckBloodEffect(UUID.randomUUID().toString(),0);
                }
                aggregate.setPercent(aggregate.getPercent() + slicing.getPercent());
                effcetMap.put("SuckBloodEffect",aggregate);
            }
        }

        // 聚合后进行效果的展示
        for (AbstractEffect effect : effcetMap.values()) {
            result.append(effect.update(character,effectTimeEnum,entity,attackList,defence));
        }
        return result;
    }
}
